package com.kxc.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 用指定频率读取牛圈沟旧数据库中的表面位移实时数据
 * @author jjq
 * @data  2024年1月31日
 * @version
 */
@Service
public class PublishDSDInfoService {
  	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	// 获取多个表面位移设备的实时信息
	public void GetDsDRealTimeDataInfo(String dsdDevNum, int dsdFrequency) {
		
		// 1、获取当前时间
		DateTime nowTime= DateTime.now();
		// 2、根据监测频率，获取上一次读取时间
		DateTime minusMinutes = nowTime.minusMinutes(dsdFrequency);
		String lastTime = minusMinutes.toString("yyyy-MM-dd HH:mm:ss");
		// 3、根据上次读取时间，获取该时间频率范围内有效的数据
		String sql = "select * from t_gpsdata t1,(SELECT MAX(GPSDATA_ID) GPSDATA_ID FROM t_gpsdata GROUP BY GPSINFO_NO) AS t2 WHERE t1.GPSDATA_ID = t2.GPSDATA_ID AND GPSDATA_TIME > '" + lastTime +"' and GPSINFO_NO in " + dsdDevNum;          
		List<Map<String,Object>> queryForList = jdbcTemplate.queryForList(sql);
		// 4、获取该表面位移设备的XYZ值，构造荷载内容
		for (Map<String, Object> map : queryForList) {
			Double x = Double.valueOf(map.get("GPSDATA_X").toString());
			Double y = Double.valueOf(map.get("GPSDATA_Y").toString());
			Double h = Double.valueOf(map.get("GPSDATA_H").toString());
			DecimalFormat df = new DecimalFormat("######0.0000");
			long timeStamp = System.currentTimeMillis();
			String json = "gnssr,devNum="+map.get("GPSINFO_NO")+" "+"xValue="+df.format(x)+","+"yValue="+df.format(y)+",zValue="+df.format(h)+" "+timeStamp;
		}
		
	}
	
}